package com.GamersCom.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.GamersCom.db.DAOFactory;
import com.GamersCom.db.Platform;
import com.GamersCom.db.PlatformDAO;

public class PlatformMgr
{
	PlatformDAO pfDao = DAOFactory.getPlatformDAO();
	
	/**
	 * Insert a platform
	 * @param pf
	 * @throws SQLException
	 */
	public void insertPlatform(Platform pf) throws SQLException 
	{
		String query = "insert into Platforms (name) values (?)";
		List paramList = new ArrayList();
		paramList.add(pf.getName());
		
		pfDao.insert(query, paramList);
	}
	
	public void delete(Platform pf) throws SQLException 
	{
		String query = "delete from platforms where name = ?";
		List paramList = new ArrayList();
		paramList.add(pf.getName());
		
		pfDao.delete(query, paramList);
	}
	
	public void update(Platform pf, Platform pfNew) throws SQLException 
	{
		String query = "update platforms set name= ? where name = ?";
		List paramList = new ArrayList();
		paramList.add(pfNew.getName());
		paramList.add(pf.getName());
		
		pfDao.update(query, paramList);
	}
	
	public List<Platform> getAll() throws SQLException
	{
		String query = "select * from platforms";		
		return pfDao.getAll(query);
	}
	
	public Platform getByName(String name) throws SQLException
	{
		String query = "select * from platforms where name = ?";
		List paramList = new ArrayList();
		paramList.add(name);
		return pfDao.getPlatform(query, paramList);
				
	}
	
	public boolean nameExists(String name)
	{
		try
		{
			if (  this.getByName(name) == null )
				return false;
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
}
